var over = 0;
var id = 0;
var destinationNode = [];    // 发货目的地
var colourList = [];         // 颜色列表
var printData = {};          // 打印数据
var maxRowNum = 0;           // 每色最大行数
var rowsPerPage = 21;        // 每页显示的行数（包含小计行）
var totalPageSize = 0;       // 总页数
var colourNumPerPage = 0;    // 每页颜色数
var coloumNumPerColour = 0;  // 每色显示列数
var pageBreakPoint = [];     // 分页点
var _w = 542;                // 每色每列组的宽度
var _w01 = 271;              // 每色每列组的宽度

var thisPage = {
    // 初始化页面
    OnPageLoad: function () {
        thisPage.PageInit();
    },
    GetFilters: function () {
        var _params = {};
        _params["HeaderId"] = parseInt(id);
        _params['CurrentUserId'] = parseInt(parent.$("#UserId").val());
        _params['WorkFlowId'] = parent.$("#WorkFlowId").val() === '' ? 0 : parseInt(parent.$("#WorkFlowId").val());
        return JSON.stringify(_params);
    },
    PageInit: function () {
        $.ajax({
            type: "POST",
            url: window.basePath + "/trade/production_instruction/print/fetch.html",
            timeout: 30 * 1000,
            data: thisPage.GetFilters(),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            }
        }).done(function (data) {
            let item = data.HeaderEntity;
            $("#SaledByName").text(item.saledByName);
            $("#ContractNo").text(item.contractNo);
            $("#SuitStyleNo").text(item.suitStyleNo);
            $("#SuitProductName").text(item.suitProductName);
            $("#IssueDate").text(item.issueDate === null ? "" : item.issueDate);
            $("#TotalQuantity").text(item.totalQuantity);
            $("#SampleQuantity").text(item.sampleQuantity);
            //$("#TradeModeName").text(item.tradeModeName);
            $("#SettlementModeName").text(item.settlementModeName);
            //let _desc = item.description === null ? "" : item.description.replace(/\n/g, '<br/>');
            let _desc = item.description === null ? ("贸易模式：" + item.tradeModeName) : ("<div style='margin-bottom:6px;'>贸易方式：" + item.tradeModeName + "</div>" + item.description.replace(/\n/g, '<br/>'));
            $("#Description").html(_desc);
            $("#PrintedDate").text(getCurrentDate());                 // 打印日期
            //let _dh = $("#header_left").height();
            //$("#Description").css({height: _dh - 20});

            // 目的地
            destinationNode = data.DestinationNode;
            // 颜色数据
            colourList = data.ColourList;
            printData = data.LinesList;
            // 每色最大行数
            maxRowNum = data.MaxRowNumPerColour;
            if (printData !== null) {
                thisPage.SetPrintPageData();
            } else {
                $('#loading').hide();
            }
        }).fail(function (data) {
            $('#loading').hide();
            ajaxError(data, 1);
        }).always(function () {
        });
    },
    // 根据颜色数决定打印页面
    SetPrintPageData: function () {
        if (colourList === null || colourList.length === 0) {
            totalPageSize = 0;
        } else {
            /*
             * 如果有发货目的地, 那么每页页面只能显示2个颜色列，后面的被目的地填充
             * 没有目的地，每个页面显示4个颜色列
             */
            if (destinationNode.length > 0) {
                if (maxRowNum < rowsPerPage) {
                    totalPageSize = Math.ceil(colourList.length / 2);
                    colourNumPerPage = 2;    // 每页颜色数
                }
                // 如果每色的行数正好等于每页显示的行数，为了给小计行留下空间，让每页显示的最大行 - 1
                if (maxRowNum === rowsPerPage) {
                    totalPageSize = colourList.length;
                    colourNumPerPage = 1;    // 每页颜色数
                }
                if (maxRowNum > rowsPerPage) {
                    totalPageSize = colourList.length;
                    colourNumPerPage = 1;    // 每页颜色数
                }
            } else {
                if (maxRowNum < rowsPerPage) {
                    totalPageSize = Math.ceil(colourList.length / 4);
                    colourNumPerPage = 4;    // 每页颜色数
                }
                // 如果每色的行数正好等于每页显示的行数，为了给小计行留下空间，让每页显示的最大行 - 1
                if (maxRowNum === rowsPerPage) {
                    totalPageSize = Math.ceil(colourList.length / 2);
                    colourNumPerPage = 2;    // 每页颜色数
                }
                if (maxRowNum > rowsPerPage) {
                    totalPageSize = Math.ceil(colourList.length / 2);
                    colourNumPerPage = 2;    // 每页颜色数
                }
            }
        }

        // 显示明细数据
        linesHandler.BuildDataTable(printData);
        $('#loading').hide();
    },
    // html打印
    DoPreview: function () {
        window.print();
        /*
         let _html_heade_content = "<!DOCTYPE html><html><head>" + window.document.head.innerHTML + "</head><body>";
         let _body_content = window.document.body.innerHTML;
         let  _print_header_start = "<!--headerstart-->";
         let _print_header_end = "<!--headerend-->";
         var _print_header_content = _body_content.substr(_body_content.indexOf(_print_header_start));
         _print_header_content = _print_header_content.substring(18, _print_header_content.indexOf(_print_header_end));
         let _print_footer_start = "<!--footerstart-->";
         let  _print_footer_end = "<!--footerend-->";
         let _html_footer_content = _body_content.substr(_body_content.indexOf(_print_footer_start));
         _html_footer_content = _html_footer_content.substring(18, _html_footer_content.indexOf(_print_footer_end));
         
         //_html_content = _html_content + _print_header_content + "<tbody>";
         let _html_body_content = "";
         $("#LineDataArea").find("tr").each(function (index) {
         _html_body_content += _print_header_content + "<tbody>";
         _html_body_content += $(this).prop("outerHTML");
         _html_body_content += "<tbody>";
         });
         let _html_content = _html_heade_content + _html_body_content + "<tbody>" + _html_footer_content + "</body></html>";
         console.info(_html_content);
         let _iframe = document.getElementById('PrintFrame');
         _iframe.innerHTML = "";   // 首先清空，然后重新写入
         let doc = _iframe.contentWindow.document;
         //doc.open();
         doc.write(_html_content);
         doc.close();
         console.info(_html_content);
         _iframe.focus();    // 获得焦点后才能打印
         //_iframe.contentWindow.focus();
         //_iframe.contentWindow.print();*/
    },
    Export: function () {
        $("#export_form").attr("action", parent.basePath + "/trade/production_instruction/export_to_excel.html");
        $("#ExportHeaderId").val(id);
        $("#export_form").submit();
    }
};

//-----------------------------------------------------------------------------
// 明细处理部分
//-----------------------------------------------------------------------------
var linesHandler = {
    BuildDataTable: function (result) {
        $("#LineDataArea").empty();
        if (totalPageSize === 0)
            return;

        // 如果有发货目的地
        if (destinationNode.length > 0) {
            let _col_width = Math.round(_w01 / destinationNode.length);
            var content = '';
            for (let p = 0; p < totalPageSize; p++) {
                //if (p < (totalPageSize - 1)) {
                //    content += '<tr class="print_lines" style="page-break-after:always"><td style="padding-left:1px;">';
                //} else {
                //    content += '<tr class="print_lines"><td style="padding-left:1px;">';
                //}
                for (var i = 0; i < colourNumPerPage; i++) {
                    // 分两种情况，一种是每色最大行大于当前页显示行，另一个是小于当前页显示行的情况
                    if (maxRowNum < rowsPerPage) {
                        content += '<div class="cell_coloumn" style="margin-left:-1px; margin-top:10px;"><table id="' + 'd-' + (p + 1) + '">';
                        content += '<tbody><tr>';
                        content += '<th class="cell" style="width: ' + (_w - _col_width * destinationNode.length - 150 - destinationNode.length + 1) + 'px;">颜色</th>';
                        content += '<th class="cell" style="width: 70px;">尺寸</th>';
                        content += '<th class="cell" style="width: 80px;">合同数</th>';
                        for (let j = 0; j < destinationNode.length; j++) {
                            content += '<th class="cell" style="width:' + _col_width + 'px;">' + destinationNode[j].destination + '</th>';
                        }
                        content += '</tr>';
                        //content += '<tbody>';
                        let firstrow = true;
                        let _currentrows = 0;
                        let _rowSubTotal = {};
                        for (var j = 0; j < result.length; j++) {
                            if (colourList[ p * colourNumPerPage + i] !== undefined && result[j].colourId === colourList[ p * colourNumPerPage + i].colourid && parseInt(result[j].rowFlag) === 0) {
                                content += '<tr>';
                                if (firstrow) {
                                    content += '<td class="cell" rowspan="' + (rowsPerPage - 1) + '">' + formatNullData(result[j].colourName) + '<br />(' + formatNullData(result[j].colourNumber) + ')</td>';
                                }
                                content += '<td class="cell">' + formatNullData(result[j].suitSizeName) + '</td>';
                                content += '<td class="cell">' + formatNullData(result[j].rowSubTotal) + '</td>';
                                for (let z = 0; z < destinationNode.length; z++) {
                                    let _destination = destinationNode[z].destination;
                                    let _t = false;
                                    $.each(result[j].schemaMemoJson, function (index, data) {
                                        if (data.Destination === _destination) {
                                            content += '<td class="cell">' + formatNullData(data.Quantity) + '</td>'; // 目的地分配数量
                                            _t = true;
                                            return false;
                                        }
                                    });
                                    if (_t === false)
                                        content += '<td class="cell"></td>'; // 目的地分配数量
                                }
                                firstrow = false;
                                content += '</tr>';
                                _currentrows = _currentrows + 1;
                            }
                            if (colourList[ p * colourNumPerPage + i] !== undefined && result[j].colourId === colourList[ p * colourNumPerPage + i].colourid && parseInt(result[j].rowFlag) === 1) {
                                _rowSubTotal = result[j];
                            }
                        }
                        // 补充空行
                        if (colourList[ p * colourNumPerPage + i] === undefined) {
                            _currentrows = 0;
                        }
                        firstrow = true;
                        for (let x = 0; x < (rowsPerPage - _currentrows - 1); x++) {
                            content += '<tr>';
                            if (colourList[ p * colourNumPerPage + i] === undefined) {
                                if (firstrow) {
                                    content += '<td class="cell" rowspan="' + (rowsPerPage - 1) + '">&nbsp;</td>';
                                    firstrow = false;
                                }
                            }
                            content += '<td class="cell">&nbsp;</td>';
                            content += '<td class="cell"></td>';
                            for (let z = 0; z < destinationNode.length; z++) {
                                content += '<td class="cell"></td>';
                            }
                            content += '</tr>';
                        }

                        // 小计行
                        content += '<tr>';
                        content += '<td class="cell" colspan="2" style="font-weight:bold;">TOTAL</td>';
                        if (colourList[ p * colourNumPerPage + i] !== undefined) {
                            content += '<td class="cell" style="font-weight:bold;">' + _rowSubTotal.rowSubTotal + '</td>';
                            for (let z = 0; z < destinationNode.length; z++) {
                                let _destination = destinationNode[z].destination;
                                let _t = false;
                                $.each(_rowSubTotal.schemaMemoJson, function (index, data) {
                                    if (data.Destination === _destination) {
                                        content += '<td class="cell" style="font-weight:bold;">' + formatNullData(data.Quantity) + '</td>'; // 目的地分配数量
                                        _t = true;
                                        return false;
                                    }
                                });
                                if (_t === false)
                                    content += '<td class="cell"></td>'; // 目的地分配数量
                            }
                        } else {
                            content += '<td class="cell" style="font-weight:bold;">&nbsp;</td>';
                            for (let z = 0; z < destinationNode.length; z++) {
                                content += '<td class="cell"></td>'; // 目的地分配数量
                            }
                        }
                        content += '</tr>';
                    } else {
                        content += '<div class="cell_coloumn" style="margin-left:-1px; margin-top:10px;"><table id="' + 'd-' + (p + 1) + '">';
                        content += '<tr>';
                        content += '<th class="cell" style="width: ' + (_w - _col_width * destinationNode.length - 150 - destinationNode.length + 1) + 'px;">颜色</th>';
                        content += '<th class="cell" style="width: 70px;">尺寸</th>';
                        content += '<th class="cell" style="width: 80px;">合同数</th>';
                        for (let j = 0; j < destinationNode.length; j++) {
                            content += '<th class="cell" style="width:' + _col_width + 'px;">' + destinationNode[j].destination + '</th>';
                        }
                        content += '</tr>';
                        content += '<tbody>';
                        let firstrow = true;
                        let _currentrows = 0;
                        let _rowSubTotal = {};  // 小计行
                        let _rowpointer = 0;  // 记录当前列表位置
                        for (var j = 0; j < result.length; j++) {
                            if (colourList[ p * colourNumPerPage + i] !== undefined && result[j].colourId === colourList[ p * colourNumPerPage + i].colourid && parseInt(result[j].rowFlag) === 0) {
                                if (_currentrows <= (rowsPerPage - 2)) {
                                    content += '<tr>';
                                    if (firstrow) {
                                        content += '<td class="cell" rowspan="' + (rowsPerPage - 1) + '">' + formatNullData(result[j].colourName) + '<br />(' + formatNullData(result[j].colourNumber) + ')</td>';
                                        firstrow = false;
                                    }
                                    content += '<td class="cell">' + formatNullData(result[j].suitSizeName) + '</td>';
                                    content += '<td class="cell">' + formatNullData(result[j].rowSubTotal) + '</td>';
                                    for (let z = 0; z < destinationNode.length; z++) {
                                        let _destination = destinationNode[z].destination;
                                        let _t = false;
                                        $.each(result[j].schemaMemoJson, function (index, data) {
                                            if (data.Destination === _destination) {
                                                content += '<td class="cell">' + formatNullData(data.Quantity) + '</td>'; // 目的地分配数量
                                                _t = true;
                                                return false;
                                            }
                                        });
                                        if (_t === false)
                                            content += '<td class="cell"></td>'; // 目的地分配数量
                                    }
                                    content += '</tr>';
                                    _currentrows += +1;
                                } else {
                                    break;  // 结束当前列以便进行下一列的显示
                                }
                            }
                            if (colourList[ p * colourNumPerPage + i] !== undefined && result[j].colourId === colourList[ p * colourNumPerPage + i].colourid && parseInt(result[j].rowFlag) === 1) {
                                _rowSubTotal = result[j];
                            }
                            _rowpointer = j;
                        }
                        // 补充空行
                        if (colourList[ p * colourNumPerPage + i] === undefined) {
                            _currentrows = 0;
                        }
                        firstrow = true;
                        for (let x = 0; x < (rowsPerPage - _currentrows - 1); x++) {
                            content += '<tr>';
                            if (colourList[ p * colourNumPerPage + i] === undefined) {
                                if (firstrow) {
                                    content += '<td class="cell" rowspan="' + (rowsPerPage - 1) + '">&nbsp;</td>';
                                    firstrow = false;
                                }
                            }
                            content += '<td class="cell">&nbsp;</td>';
                            content += '<td class="cell"></td>';
                            for (let z = 0; z < destinationNode.length; z++) {
                                content += '<td class="cell"></td>';
                            }
                            content += '</tr>';
                        }
                        // 小计行
                        if (!$.isEmptyObject(_rowSubTotal)) {
                            content += '<tr>';
                            content += '<td class="cell" colspan="2" style="font-weight:bold;">TOTAL</td>';
                            if (colourList[ p * colourNumPerPage + i] !== undefined) {
                                content += '<td class="cell" style="font-weight:bold;">' + _rowSubTotal.rowSubTotal + '</td>';
                                for (let z = 0; z < destinationNode.length; z++) {
                                    let _destination = destinationNode[z].destination;
                                    let _t = false;
                                    $.each(_rowSubTotal.schemaMemoJson, function (index, data) {
                                        if (data.Destination === _destination) {
                                            content += '<td class="cell" style="font-weight:bold;">' + formatNullData(data.Quantity) + '</td>'; // 目的地分配数量
                                            _t = true;
                                            return false;
                                        }
                                    });
                                    if (_t === false)
                                        content += '<td class="cell"></td>'; // 目的地分配数量
                                }
                            } else {
                                content += '<td class="cell" style="font-weight:bold;">&nbsp;</td>';
                                for (let z = 0; z < destinationNode.length; z++) {
                                    content += '<td class="cell"></td>'; // 目的地分配数量
                                }
                            }
                            content += '</tr>';
                            _rowSubTotal = {};
                        } else {
                            content += '<tr>';
                            content += '<td class="cell" colspan="2" style="font-weight:bold;">TOTAL</td>';
                            content += '<td class="cell" style="font-weight:bold;">&nbsp;</td>';
                            for (let z = 0; z < destinationNode.length; z++) {
                                content += '<td class="cell"></td>'; // 目的地分配数量
                            }
                        }

                        content += '</tbody></table></div>';

                        // 开始设置该色的第二列
                        content += '<div class="cell_coloumn" style="margin-left:-1px; margin-top:10px;"><table id="' + 'd-' + (p + 1) + '">';
                        content += '<tbody><tr>';
                        content += '<th class="cell" style="width: ' + (_w - _col_width * destinationNode.length - 150 - destinationNode.length + 1) + 'px;">颜色</th>';
                        content += '<th class="cell" style="width: 70px;">尺寸</th>';
                        content += '<th class="cell" style="width: 80px;">合同数</th>';
                        for (let j = 0; j < destinationNode.length; j++) {
                            content += '<th class="cell" style="width:' + _col_width + 'px;">' + destinationNode[j].destination + '</th>';
                        }
                        content += '</tr>';

                        // 接上一列开始
                        firstrow = true;
                        _currentrows = 0;
                        for (let j = (_rowpointer + 1); j < result.length; j++) {
                            if (colourList[ p * colourNumPerPage + i] !== undefined && result[j].colourId === colourList[ p * colourNumPerPage + i].colourid && parseInt(result[j].rowFlag) === 0) {
                                content += '<tr>';
                                if (firstrow) {
                                    content += '<td class="cell" rowspan="' + (rowsPerPage - 1) + '">' + formatNullData(result[j].colourName) + '<br />(' + formatNullData(result[j].colourNumber) + ')</td>';
                                    firstrow = false;
                                }
                                content += '<td class="cell">' + formatNullData(result[j].suitSizeName) + '</td>';
                                content += '<td class="cell">' + formatNullData(result[j].rowSubTotal) + '</td>';
                                for (let z = 0; z < destinationNode.length; z++) {
                                    let _destination = destinationNode[z].destination;
                                    let _t = false;
                                    $.each(result[j].schemaMemoJson, function (index, data) {
                                        if (data.Destination === _destination) {
                                            content += '<td class="cell">' + formatNullData(data.Quantity) + '</td>'; // 目的地分配数量
                                            _t = true;
                                            return false;
                                        }
                                    });
                                    if (_t === false)
                                        content += '<td class="cell"></td>'; // 目的地分配数量
                                }
                                content += '</tr>';
                                _currentrows += +1;
                            }
                            if (colourList[ p * colourNumPerPage + i] !== undefined && result[j].colourId === colourList[ p * colourNumPerPage + i].colourid && parseInt(result[j].rowFlag) === 1) {
                                _rowSubTotal = result[j];
                            }
                        }
                        // 补充空行
                        if (colourList[ p * colourNumPerPage + i] === undefined) {
                            _currentrows = 0;
                        }
                        firstrow = true;
                        for (let x = 0; x < (rowsPerPage - _currentrows - 1); x++) {
                            content += '<tr>';
                            if (colourList[ p * colourNumPerPage + i] !== undefined && _currentrows === 0) {
                                if (firstrow) {
                                    content += '<td class="cell" rowspan="' + (rowsPerPage - 1) + '">&nbsp;</td>';
                                    firstrow = false;
                                }
                            }
                            content += '<td class="cell">&nbsp;</td>';
                            content += '<td class="cell"></td>';
                            for (let z = 0; z < destinationNode.length; z++) {
                                content += '<td class="cell"></td>';
                            }
                            content += '</tr>';
                        }

                        // 小计行
                        if (!$.isEmptyObject(_rowSubTotal)) {
                            content += '<tr>';
                            content += '<td class="cell" colspan="2" style="font-weight:bold;">TOTAL</td>';
                            if (colourList[ p * colourNumPerPage + i] !== undefined) {
                                content += '<td class="cell" style="font-weight:bold;">' + _rowSubTotal.rowSubTotal + '</td>';
                                for (let z = 0; z < destinationNode.length; z++) {
                                    let _destination = destinationNode[z].destination;
                                    let _t = false;
                                    $.each(_rowSubTotal.schemaMemoJson, function (index, data) {
                                        if (data.Destination === _destination) {
                                            content += '<td class="cell" style="font-weight:bold;">' + formatNullData(data.Quantity) + '</td>'; // 目的地分配数量
                                            _t = true;
                                            return false;
                                        }
                                    });
                                    if (_t === false)
                                        content += '<td class="cell"></td>'; // 目的地分配数量
                                }
                            } else {
                                content += '<td class="cell" style="font-weight:bold;">&nbsp;</td>';
                                for (let z = 0; z < destinationNode.length; z++) {
                                    content += '<td class="cell"></td>'; // 目的地分配数量
                                }
                            }
                            content += '</tr>';
                        } else {
                            content += '<tr>';
                            content += '<td class="cell" colspan="2" style="font-weight:bold;">TOTAL</td>';
                            content += '<td class="cell" style="font-weight:bold;">&nbsp;</td>';
                            for (let z = 0; z < destinationNode.length; z++) {
                                content += '<td class="cell"></td>'; // 目的地分配数量
                            }
                        }
                        content += '</tr>';
                    }
                    content += '</tbody></table></div>';
                }
            }
            content += '</td></tr>';
            $("#LineDataArea").append(content);
            content = '';
            //if (p < (totalPageSize - 1))
            //    $("#LineDataArea").append('<tr class="page-break print_lines"><td style="padding-top:10px;">-- 分页符 --</td></tr>');
        } else {
            var content = '';
            for (var p = 0; p < totalPageSize; p++) {
                //if (p < (totalPageSize - 1)) {
                content += '<tr class="print_lines" style="page-break-after:always;"><td style="padding-left:1px;">';
                //} else {
                //    content += '<tr class="print_lines"><td style="padding-left:1px;">';
                //}
                for (var i = 0; i < colourNumPerPage; i++) {
                    // 分两种情况，一种是每色最大行大于当前页显示行，另一个是小于当前页显示行的情况
                    if (maxRowNum < rowsPerPage) {
                        content += '<div class="cell_coloumn" style="margin-left:-1px; margin-top:10px;"><table id="' + 'd-' + (p + 1) + '">';
                        content += '<tbody><tr>';
                        content += '<th class="cell" style="width: ' + (_w01 - 150) + 'px;">颜色</th>';
                        content += '<th class="cell" style="width: 70px;">尺寸</th>';
                        content += '<th class="cell" style="width: 80px;">合同数</th>';
                        content += '</tr>';
                        //content += '';
                        let firstrow = true;
                        let _currentrows = 0;
                        let _rowSubTotal = {};  // 小计行
                        for (var j = 0; j < result.length; j++) {
                            if (colourList[ p * colourNumPerPage + i] !== undefined && result[j].colourId === colourList[ p * colourNumPerPage + i].colourid && parseInt(result[j].rowFlag) === 0) {
                                content += '<tr>';
                                if (firstrow) {
                                    content += '<td class="cell" rowspan="' + (rowsPerPage - 1) + '">' + formatNullData(result[j].colourName) + '<br />(' + formatNullData(result[j].colourNumber) + ')</td>';
                                }
                                content += '<td class="cell">' + formatNullData(result[j].suitSizeName) + '</td>';
                                content += '<td class="cell">' + formatNullData(result[j].rowSubTotal) + '</td>';
                                firstrow = false;
                                content += '</tr>';
                                _currentrows = _currentrows + 1;
                            }
                            if (colourList[ p * colourNumPerPage + i] !== undefined && result[j].colourId === colourList[ p * colourNumPerPage + i].colourid && parseInt(result[j].rowFlag) === 1) {
                                _rowSubTotal = result[j];
                            }
                        }
                        // 补充空行
                        if (colourList[ p * colourNumPerPage + i] === undefined) {
                            _currentrows = 0;
                        }
                        firstrow = true;
                        for (let x = 0; x < (rowsPerPage - _currentrows - 1); x++) {
                            content += '<tr>';
                            if (colourList[ p * colourNumPerPage + i] === undefined) {
                                if (firstrow) {
                                    content += '<td class="cell" rowspan="' + (rowsPerPage - 1) + '">&nbsp;</td>';
                                    firstrow = false;
                                }
                            }
                            content += '<td class="cell">&nbsp;</td>';
                            content += '<td class="cell"></td>';
                            content += '</tr>';
                        }

                        // 小计行
                        if (!$.isEmptyObject(_rowSubTotal)) {
                            content += '<tr>';
                            content += '<td class="cell" colspan="2" style="font-weight:bold;">TOTAL</td>';
                            if (colourList[ p * colourNumPerPage + i] !== undefined) {
                                content += '<td class="cell" style="font-weight:bold;">' + _rowSubTotal.rowSubTotal + '</td>';
                            } else {
                                content += '<td class="cell" style="font-weight:bold;">&nbsp;</td>';
                            }
                            content += '</tr>';
                        } else {
                            content += '<tr>';
                            content += '<td class="cell" colspan="2" style="font-weight:bold;">TOTAL</td>';
                            content += '<td class="cell" style="font-weight:bold;">&nbsp;</td>';
                            content += '</tr>';
                        }
                        content += '</tbody></table></div>';
                    } else {
                        content += '<div class="cell_coloumn" style="margin-left:-1px; margin-top:10px;"><table id="' + 'd-' + (p + 1) + '">';
                        content += '<tbody><tr>';
                        content += '<th class="cell" style="width: ' + (_w01 - 150) + 'px;">颜色</th>';
                        content += '<th class="cell" style="width: 70px;">尺寸</th>';
                        content += '<th class="cell" style="width: 80px;">合同数</th>';
                        content += '</tr>';
                        //content += '<tbody>';
                        let firstrow = true;
                        let _currentrows = 0;
                        let _rowSubTotal = {};  // 小计行
                        let _rowpointer = 0;    // 记录当前列表位置
                        for (var j = 0; j < result.length; j++) {
                            if (colourList[ p * colourNumPerPage + i] !== undefined && result[j].colourId === colourList[ p * colourNumPerPage + i].colourid && parseInt(result[j].rowFlag) === 0) {
                                if (_currentrows <= (rowsPerPage - 2)) {
                                    content += '<tr>';
                                    if (firstrow) {
                                        content += '<td class="cell" rowspan="' + (rowsPerPage - 1) + '">' + formatNullData(result[j].colourName) + '<br />(' + formatNullData(result[j].colourNumber) + ')</td>';
                                        firstrow = false;
                                    }
                                    content += '<td class="cell">' + formatNullData(result[j].suitSizeName) + '</td>';
                                    content += '<td class="cell">' + formatNullData(result[j].rowSubTotal) + '</td>';
                                    content += '</tr>';
                                    _currentrows += +1;
                                } else {
                                    break;  // 结束当前列以便进行下一列的显示
                                }
                            }
                            if (colourList[ p * colourNumPerPage + i] !== undefined && result[j].colourId === colourList[ p * colourNumPerPage + i].colourid && parseInt(result[j].rowFlag) === 1) {
                                _rowSubTotal = result[j];
                            }
                            _rowpointer = j;
                        }
                        // 补充空行
                        if (colourList[ p * colourNumPerPage + i] === undefined) {
                            _currentrows = 0;
                        }
                        firstrow = true;
                        for (let x = 0; x < (rowsPerPage - _currentrows - 1); x++) {
                            content += '<tr>';
                            if (colourList[ p * colourNumPerPage + i] === undefined) {
                                if (firstrow) {
                                    content += '<td class="cell" rowspan="' + (rowsPerPage - 1) + '">&nbsp;</td>';
                                    firstrow = false;
                                }
                            }
                            content += '<td class="cell">&nbsp;</td>';
                            content += '<td class="cell"></td>';
                            content += '</tr>';
                        }
                        // 小计行
                        if (!$.isEmptyObject(_rowSubTotal)) {
                            content += '<tr>';
                            content += '<td class="cell" colspan="2" style="font-weight:bold;">TOTAL</td>';
                            if (colourList[ p * colourNumPerPage + i] !== undefined) {
                                content += '<td class="cell" style="font-weight:bold;">' + _rowSubTotal.rowSubTotal + '</td>';
                            } else {
                                content += '<td class="cell" style="font-weight:bold;">&nbsp;</td>';
                            }
                            content += '</tr>';
                            _rowSubTotal = {};
                        } else {
                            content += '<tr>';
                            content += '<td class="cell" colspan="2" style="font-weight:bold;">TOTAL</td>';
                            content += '<td class="cell" style="font-weight:bold;">&nbsp;</td>';
                            content += '</tr>';
                        }
                        content += '</tbody></table></div>';

                        // 设置第二列
                        content += '<div class="cell_coloumn" style="margin-left:-1px; margin-top:10px;"><table id="' + 'd-' + (p + 1) + '">';
                        content += '<tbody><tr>';
                        content += '<th class="cell" style="width: ' + (_w01 - 150) + 'px;">颜色</th>';
                        content += '<th class="cell" style="width: 70px;">尺寸</th>';
                        content += '<th class="cell" style="width: 80px;">合同数</th>';
                        content += '</tr>';
                        //content += '<tbody>';

                        // 接上一列开始
                        firstrow = true;
                        _currentrows = 0;
                        for (var j = (_rowpointer + 1); j < result.length; j++) {
                            if (colourList[ p * colourNumPerPage + i] !== undefined && result[j].colourId === colourList[ p * colourNumPerPage + i].colourid && parseInt(result[j].rowFlag) === 0) {
                                content += '<tr>';
                                if (firstrow) {
                                    content += '<td class="cell" rowspan="' + (rowsPerPage - 1) + '">' + formatNullData(result[j].colourName) + '<br />(' + formatNullData(result[j].colourNumber) + ')</td>';
                                    firstrow = false;
                                }
                                content += '<td class="cell">' + formatNullData(result[j].suitSizeName) + '</td>';
                                content += '<td class="cell">' + formatNullData(result[j].rowSubTotal) + '</td>';
                                content += '</tr>';
                                _currentrows += +1;
                            }
                            if (colourList[ p * colourNumPerPage + i] !== undefined && result[j].colourId === colourList[ p * colourNumPerPage + i].colourid && parseInt(result[j].rowFlag) === 1) {
                                _rowSubTotal = result[j];
                            }
                        }
                        // 补充空行
                        if (colourList[ p * colourNumPerPage + i] === undefined) {
                            _currentrows = 0;
                        }
                        firstrow = true;
                        for (let x = 0; x < (rowsPerPage - _currentrows - 1); x++) {
                            content += '<tr>';
                            if (colourList[ p * colourNumPerPage + i] !== undefined && _currentrows === 0) {
                                if (firstrow) {
                                    content += '<td class="cell" rowspan="' + (rowsPerPage - 1) + '">&nbsp;</td>';
                                    firstrow = false;
                                }
                            }
                            content += '<td class="cell">&nbsp;</td>';
                            content += '<td class="cell">&nbsp;</td>';
                            content += '</tr>';
                        }

                        // 小计行
                        if (!$.isEmptyObject(_rowSubTotal)) {
                            content += '<tr>';
                            content += '<td class="cell" colspan="2" style="font-weight:bold;">TOTAL</td>';
                            if (colourList[ p * colourNumPerPage + i] !== undefined) {
                                content += '<td class="cell" style="font-weight:bold;">' + _rowSubTotal.rowSubTotal + '</td>';
                            } else {
                                content += '<td class="cell" style="font-weight:bold;">&nbsp;</td>';
                            }
                            content += '</tr>';
                            _rowSubTotal = {};
                        } else {
                            content += '<tr>';
                            content += '<td class="cell" colspan="2" style="font-weight:bold;">TOTAL</td>';
                            content += '<td class="cell" style="font-weight:bold;">&nbsp;</td>';
                            content += '</tr>';
                        }
                        content += '</tbody></table></div>';
                    }
                }
                content += '</td></tr>';
                $("#LineDataArea").append(content);
                content = '';
                //if (p < (totalPageSize - 1))
                //    $("#LineDataArea").append('<tr class="page-break print_lines"><td style="padding-top:10px;">-- 分页符 --</td></tr>');
            }
        }
    },
    InsertPageBreak: function () {
        if (pageBreakPoint.length > 0 && pageBreakPoint[0] === linesHandler._rows) {
            pageBreakPoint = [];
        }
        if ($("#tabData tr.row__clicked").length > 0) {
            var content = $('<tr class="page-break"><td colspan="11" style="text-align:center;"><p>-- 分页符 --</p></td></tr>');
            $("#tabData tr.row__clicked").after(content);
            // 增加分页点
            pageBreakPoint.push(parseInt($("#tabData tr.row__clicked").attr("sequence")));
        } else {
            ShowErrorMessagePanel('请选择要进行分页的位置。');
            $(".error-msg").css({top: '2px'});
        }
    }
};
//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    id = jQuery.url.param("id");

    // 用于导出excel文件
    $("#export_iframe").remove();
    let $iFrame = $('<iframe id="export_iframe" name="_export_iframe" style="position:absolute; top:-9999px; left:-9999px" src="javascript:false"></iframe>');
    $("body").append($iFrame);
    let $form = $('<form id="export_form" name ="_export_form" method="POST" action ="" target="_export_iframe" style="position:absolute; top:-1200px; left:-1200px"></form>');
    $('<input type="hidden" id="ExportHeaderId" name="_ExportHeaderId" value="" />').appendTo($form);
    $("body").append($form);
    // 监听iframe回调（submit()后设置iFrame加载完成事件，用于获取表单提交成功后的结果,iFrame.load(loadFunc());）
    $iFrame.on("load", function () {
        let contentWin = document.getElementById("export_iframe").contentWindow;
        let backBodyText = contentWin.document.body.innerText;
    });

    thisPage.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
}
